package cn.edu.gdu.quizpractice.controller.mini;

import cn.edu.gdu.quizpractice.entity.ApiResult;
import cn.edu.gdu.quizpractice.service.ExternalApiService;
import io.micrometer.core.annotation.Timed;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * @Author: Aubery
 * @Date: 2025-04-14-23:39
 * @Description: 外部API调用控制器
 */
@RestController
@RequestMapping("/mini/externalAPI")
@Timed(value = "mini/externalAPI",description = "APIController")
@Slf4j
public class APIController {

    @Autowired
    private ExternalApiService externalApiService;

    /**
     * 获取每日一句
     * @param rand 是否随机，0-不随机，1-随机
     * @param date 指定日期，格式：yyyy-MM-dd，默认当天
     * @return ONE一个每日一句
     */
    @GetMapping("/dailyOne")
    public ApiResult getDailyOne(
            @RequestParam(required = false, defaultValue = "0") Integer rand,
            @RequestParam(required = false) String date) {
        try {
            Map<String, Object> result = externalApiService.getDailyOne(rand, date);
            return ApiResult.success(result);
        } catch (Exception e) {
            log.error("调用每日一句API出错: ", e);
            return ApiResult.fail("调用每日一句API出错: " + e.getMessage());
        }
    }
    
    /**
     * 获取天气信息
     * @param city 城市名称
     * @return 天气信息
     */
    @GetMapping("/weather")
    public ApiResult getWeather(@RequestParam String city) {
        try {
            Map<String, Object> result = externalApiService.getWeather(city);
            return ApiResult.success(result);
        } catch (Exception e) {
            log.error("调用天气API出错: ", e);
            return ApiResult.fail("调用天气API出错: " + e.getMessage());
        }
    }
    
    /**
     * 获取新闻头条
     * @param type 新闻类型，如：top(头条)、guonei(国内)、guoji(国际)等
     * @return 新闻列表
     */
    @GetMapping("/news")
    public ApiResult getNews(@RequestParam(required = false, defaultValue = "top") String type) {
        try {
            Map<String, Object> result = externalApiService.getNews(type);
            return ApiResult.success(result);
        } catch (Exception e) {
            log.error("调用新闻API出错: ", e);
            return ApiResult.fail("调用新闻API出错: " + e.getMessage());
        }
    }
    
    // 可以继续添加更多API调用方法...
}
